package com.yq.admin.action;

import com.yq.admin.service.ProductFrontService;
import com.yq.admin.util.AdminConstants;
import com.yq.admin.vo.ProductVO;
import com.yq.api.comm.AjaxResult;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yq on 16-8-13.
 */
@Controller("productAction")
public class ProductAction {
    @Resource(name = "productAdminService")
    private ProductFrontService productFrontService;

    @RequestMapping("gotoProductManage.do")
    public String gotoProductManage(){
        return AdminConstants.PRODUCT_MANAGE_PATH;
    }

    @RequestMapping("gotoAddProduct")
    public String gotoAddProduct(){
        return AdminConstants.ADD_PRODUCT_PATH;
    }

    /**
     * 添加产品，异步请求
     * */
    @RequestMapping("addProduct.do")
    @ResponseBody
    public AjaxResult addProduct(ProductVO productVO){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            productFrontService.addProduct(productVO);
        } catch (Exception e) {
            ajaxResult.setMessage(e.getMessage());
            ajaxResult.setSuccess(Boolean.FALSE);
            return ajaxResult;
        }
        Map<String, Object> infoDataMap = ajaxResult.getInfoData();
        if(MapUtils.isEmpty(infoDataMap)){
            infoDataMap = new HashMap<String, Object>();
        }
        infoDataMap.put("product", productVO);
        return ajaxResult;
    }
}
